
/****************************************************************/
/**********   0. Intros   ***************************************/
/****************************************************************/
set more off
global path ""

/****************************************************************/
/**********   1. Graph for the U.S.  *****************************/
/****************************************************************/

	use "$path/2_IntermediateFiles/Master_CapitalIQ_Compustat.dta", clear
	
		*Sample
		keep if fic == "USA"
		
		*Footnote in Section 3 (Europe): statements about firm size
		tabstat at , s(p25 median mean p75)
		table year, c(sum at)
		table year if at > 300, c(sum at)
		
		*Collapse
		collapse (sum) at che undrawncrdtportionrevolvingcrdt outstandingbalrrevolvingcredit srbondsandnotes generalotherborrowings outstandingbaltermloans totoutstbalcommercialpaper outstandingbalcapitalleases subordinatedbondsandnotes (mean) GDP , by(year)
			
		/*Percentage relative to GDP*/
			gen fractionsrbonds = srbondsandnotes/GDP
			gen fractioncompaper = totoutstbalcommercialpaper/GDP
			gen fractionsubordinated = subordinatedbondsandnotes/GDP
			
			gen fractiontermloans = outstandingbaltermloans/GDP
			gen fractionrevolver_drawn = outstandingbalrrevolvingcredit/GDP
			
			gen fractionrevolver_undrawn = undrawncrdtportionrevolvingcrdt/GDP
			
			gen fractionleases = outstandingbalcapitalleases/GDP

				*Aggregates across items
				gen bankdebt_onbalance = fractiontermloans + fractionrevolver_drawn
				gen bankdebt_onandoffbalance = bankdebt_onbalance + fractionrevolver_undrawn
				gen revolverdebt = fractionrevolver_drawn + fractionrevolver_undrawn
				gen bonddebt = fractionsrbonds + fractioncompaper + fractionsubordinated
			
		/*Relative to total assets*/
			gen fractionsrbonds_ta = srbondsandnotes/at
			gen fractioncompaper_ta = totoutstbalcommercialpaper/at
			gen fractionsubordinated_ta = subordinatedbondsandnotes/at
			
			gen fractiontermloans_ta = outstandingbaltermloans/at
			gen fractionrevolver_drawn_ta = outstandingbalrrevolvingcredit/at
			
			gen fractionrevolver_undrawn_ta = undrawncrdtportionrevolvingcrdt/at
			
			gen fractionleases_ta = outstandingbalcapitalleases/at
			
				*Aggregates across items
				gen bankdebt_onbalance_ta = fractiontermloans_ta + fractionrevolver_drawn_ta
				gen bankdebt_onandoffbalance_ta = bankdebt_onbalance_ta + fractionrevolver_undrawn_ta
				gen revolverdebt_ta = fractionrevolver_drawn_ta + fractionrevolver_undrawn_ta
				gen bonddebt_ta = fractionsrbonds_ta + fractioncompaper_ta + fractionsubordinated_ta
			
		*Cash holdings
			gen che_gdp = che / GDP
			gen che_ta = che / at
		
		/* Loan/Bond-ratio */
		gen ratioloanbond = bankdebt_onandoffbalance / bonddebt
		
		/*Figure*/
		clonevar yearx=year
		replace yearx = year+0.3
		
		clonevar yearxx=year
		replace yearxx = year+0.6
				
		#d ;
		twoway (bar bankdebt_onbalance  year, color (gs6) barwidth(0.3)  yaxis(1))
				(rbar bankdebt_onbalance bankdebt_onandoffbalance year, color (gs9) barwidth(0.3)  yaxis(1))	
				(line bonddebt year, color(gs0) yaxis(2))
				,graphregion(fcolor(white) lcolor(white) ifcolor(white) ilcolor(white))
				legend(pos(6) ring(50) col(1) order(1 "Term loans" 2 "Undrawn credit lines" 3 "Bonds"))
				xtitle("Year") 
				xlabel(2002(1)2019, labsize(small) angle(vertical)) xmtick(2002(1)2019, nolabels ticks)
				ytitle("Bank Loans (% GDP)", axis(1))
				ytitle("Bonds (% GDP)", axis(2))
				ylabel(0.00(0.05)0.1, labsize(small) angle(90) nogrid axis(1))
				ylabel(0.15(0.05)0.25, labsize(small) angle(90) nogrid axis(2))			
				;
		#d cr
		graph	export "Figure 1 Panel A.png", as(png) name("Graph"), replace
